package com.peach.stock;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.client.RestTemplate;

import com.peach.stock.mapper.StockMapper;
import com.peach.stock.service.AnalysisService;
import com.peach.stock.service.DataAPIService;

@SpringBootApplication
@EnableScheduling
@MapperScan({"com.peach.stock.mapper"})
public class Application implements CommandLineRunner {

	private static final Logger log = LoggerFactory.getLogger(Application.class);
	
	@Autowired
    private DataAPIService dataAPIService;
	
	@Autowired
    private AnalysisService analysisService;
	
	
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
//		ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
//		System.out.println(run.getEnvironment().getProperty("server.port"));
        
    }

//	String url1 = "http://www.szse.cn/api/report/ShowReport/data?SHOWTYPE=JSON&CATALOGID=1110x&TABKEY=tab2&PAGENO= {1}1&random=0.{2}";
	// 周一至周五下午4点30分执行
	@Scheduled(cron = "0 30 18 * * 1,2,3,4,5")
//	@Scheduled(cron = "0 3 9 * * 2")
	public void cron() {

//		String sz = restTemplate.getForEntity(url1, String.class, "1", System.currentTimeMillis()).getBody();
//		String sz = restTemplate.getForEntity("http://www.szse.cn/api/report/ShowReport/data?"
//				+ "SHOWTYPE=JSON&CATALOGID=1110x&TABKEY=tab2&PAGENO=1&random=0.6612030483790758", String.class).getBody();
//	    System.out.println(sz);
		
		log.info("######## start getHQ ... ########");
		SimpleDateFormat SDF10 = new SimpleDateFormat("yyyy-MM-dd");
		String rq = SDF10.format(new Date());
		Process proc;
        try {
//        	D:\program\Python37\test
//            proc = Runtime.getRuntime().exec("python3 D:\\program\\Python37\\test\\test02.py");
        	proc = Runtime.getRuntime().exec(String.format("python3 /opt/stock/python/stock_01.py %s %s", rq, rq));
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line = null;
            while ((line = in.readLine()) != null) {
            	log.info(line);
            }
            in.close();
            proc.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } 
//		dataAPIService.getHQ(rq);
		
		log.info("######## getHQ over !!! ########");
	}

	@Override
	public void run(String... args) throws Exception {
		// TODO Auto-generated method stub
		log.info("######## start success !!!! ########");
//		dataAPIService.szse();
		
//		analysisService.reportWeek();
		long t = System.currentTimeMillis();
//		analysisService.getDownTop(5);
		log.info("user time : " + (System.currentTimeMillis() - t));
		
	}

}
